package com.zmn.plat.admin.controller.service.tariff;

import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.plat.common.dictionary.ProductDict;
import com.zmn.plat.model.entity.service.group.ServiceGroup;
import com.zmn.plat.model.entity.service.tariff.ServiceTariff;
import com.zmn.plat.model.entity.service.tariff.ServiceTariffQuery;
import com.zmn.plat.model.vo.service.group.ServiceGroupDetailVO;
import com.zmn.plat.model.vo.service.tariff.ServiceTariffVO;

/**
 * 故障价格名称表，统一名称管理用
 *
 * @author tanbiao
 * @since 2019-11-21 18:56:50
 */
@RequestMapping("/service/tariff/")
@Controller
public class ServiceTariffListController extends ServiceTariffBaseController {

    private static final long serialVersionUID = -1793212827366295802L;

    /**
     * 列表
     *
     * @param query
     * @author tanbiao
     * @since 2019-11-21 18:56:50
     */
    @RequestMapping("list")
    @RequiresPermissions("/service/tariff/list.action")
    public ModelAndView list(@ModelAttribute ServiceTariffQuery query) {

        List<ServiceTariff> items = faultTariffService.listPageByQuery(query);

        ModelAndView mav = new ModelAndView("service/tariff/list");
        mav.addObject("items", items);
        mav.addObject("vo", query);
        mav.addObject("statuses", GlobalDict.getStatusList(false));
        mav.addObject("bizTypeList", ProductDict.listBizType());
        return mav;
    }

    /**
     * 详情
     *
     * @param tariffId
     * @author tanbiao
     * @since 2019-11-21 18:56:50
     */
    @RequestMapping("detail")
    @RequiresPermissions("/service/tariff/detail.action")
    public ModelAndView detail(@RequestParam(value = "tariffId", defaultValue = "0") Integer tariffId,
        @RequestParam(value = "flag", defaultValue = "1") Integer flag) {
        ServiceTariff faultTariff = faultTariffBService.findByKey(tariffId);
        ModelAndView mav = new ModelAndView("service/tariff/detail");
        mav.addObject("faultTariff", faultTariff);
        mav.addObject("flag", flag);
        return mav;
    }

    /**
     * 拷贝价格表
     *
     * @param tariffId
     * @author tanbiao
     * @since 2019-11-21 18:56:50
     */
    @RequestMapping("copy")
    @RequiresPermissions("/service/tariff/copy.action")
    public ModelAndView copy(@RequestParam(value = "tariffId", defaultValue = "0") Integer tariffId) {
        ModelAndView mav = new ModelAndView("service/tariff/copy");
        List<ServiceTariff> listAll = faultTariffService.listAll();
        // 原价格表
        mav.addObject("organTariffs", listAll);
        return mav;
    }

    /**
     * 原价格表联动:保内/保外、价格表类型、定价价方式都必须相同；只能复制通用产品的价格表
     * 
     * @param tariffId
     * @author tanbiao
     * @since 2019-11-21 18:56:50
     */
    @RequestMapping("loadTariff")
    @RequiresPermissions("/service/tariff/copy.action")
    @ResponseBody
    public ResultDTO<List<ServiceTariff>> loadTariff(@RequestParam(value = "tariffId", defaultValue = "0") Integer tariffId) {
        ServiceTariff tariff = faultTariffService.findByKey(tariffId);
        ServiceTariffQuery query = new ServiceTariffQuery();
        query.setBizType(tariff.getBizType());
        query.setTariffType(tariff.getTariffType());
        query.setPriceType(tariff.getPriceType());
        List<ServiceTariff> copyTariff = faultTariffService.listByQuery(query);
        if (!CollectionUtils.isEmpty(copyTariff)) {
            copyTariff = copyTariff.stream().filter(e -> !e.getTariffId().equals(tariffId)).collect(Collectors.toList());
        }
        return ResultDTO.success(copyTariff);
    }

    /**
     * 价格表联动服务分类
     * 
     * @param tariffId
     * @author tanbiao
     * @since 2020-03-04 18:57:24
     */
    @RequestMapping("loadSerCategory")
    @ResponseBody
    public ResultDTO<List<ServiceGroupDetailVO>> loadSerCategory(@RequestParam("tariffId") Integer tariffId) {
        // 加载当前价格表下的服务分类
        List<ServiceGroupDetailVO> servCategories = faultGroupService.listServCategoryByTariffId(tariffId);
        return ResultDTO.success(servCategories);
    }

    /**
     * 价格表联动服务分类加载后台产品一级分类
     * 
     * @param tariffId
     * @author tanbiao
     * @since 2020-03-04 18:57:24
     */
    @RequestMapping("loadBaseCategory")
    @ResponseBody
    public ResultDTO<List<ServiceGroupDetailVO>> loadBaseCategory(@RequestParam("tariffId") Integer tariffId,
        @RequestParam("servCategId") Integer servCategId) {
        // 加载当前价格表下的服务分类的后台一级分类
        List<ServiceGroupDetailVO> servCategories = faultGroupService.listBaseCategoryByTariffIdAndServCategId(tariffId, servCategId);
        return ResultDTO.success(servCategories);
    }

    /**
     * 描述:检查价格表是否存在通用产品
     *
     * @param faultTariffVO
     * @return: ResultDTO 执行结果
     * @auther: tanbiao
     * @since:20200305
     */
    @RequestMapping(value = "check")
    @RequiresPermissions("/service/tariff/copy.action")
    @ResponseBody
    public ResponseDTO<Boolean> checkHasCommonProudctPriceTable(HttpServletRequest request, @RequestBody ServiceTariffVO faultTariffVO) {

        if (faultTariffVO.getOrganTariffId() == null || faultTariffVO.getNewTariffId() == null) {
            return ResponseDTO.fail("请选择价格表！");
        }
        if (faultTariffVO.getServCategId() == null) {
            return ResponseDTO.fail("请选择分类！");
        }
        List<ServiceGroup> list = faultTariffBService.listGroupByTariffIdAndServCategIdAndCategOneId(faultTariffVO.getNewTariffId(),
            faultTariffVO.getServCategId(), faultTariffVO.getCategOneId());
        return ResponseDTO.success(CollectionUtils.isEmpty(list));
    }

}
